/**
 * 游戏数据
 */
var playerPerfers = require('PlayerPerfers');
var wxInterfaxe   = require('wxInterface')
 var UserData = cc.Class({
    statics: {
        instance: null
    },
    extends: cc.Component,

    properties: {
        isLogined:false,
        isGetedInfo:false,
        histroyScore:0,  //历史最高分数
        curScore:0,      //当前分数

        nickName:"",
        gender:"",
        avatarUrl:"",
        isQMScreen:false,
    },

    // LIFE-CYCLE CALLBACKS:

    ctor() {
        this.isQMScreen = false;
        // playerPerfers.instance.clear();
        this.arrData_2 = [];
        this.readData();
    },
    readData(){
        var res = playerPerfers.instance.getItem("score");
        if(res)
            this.histroyScore = parseInt(res);

        // if(this.histroyScore > 0){
            //将数据存储到微信
            wxInterfaxe.instance.setWXData(this.histroyScore, this.histroyScore);
        // }
        this.nickName = playerPerfers.instance.getItem("nickName");
        this.gender   = playerPerfers.instance.getItem("gender");
        this.avatarUrl= playerPerfers.instance.getItem("avatarUrl");
        // console.log("********** " + this.nickName);
    },

    saveData () {
        playerPerfers.instance.setItem("score",    this.histroyScore);
        playerPerfers.instance.setItem("nickName", this.nickName);
        playerPerfers.instance.setItem("gender",   this.gender);
        playerPerfers.instance.setItem("avatarUrl",this.avatarUrl);
        
        //将数据存储到微信
        wxInterfaxe.instance.setWXData(this.histroyScore, this.histroyScore);
    },
    //
    readArrData(){
        var arrdata_2 = playerPerfers.instance.getItem("arrdata_2");
        if(arrdata_2){
            var obj = JSON.parse(arrdata_2);
            if(arrdata_2){
                this.arrData_2 = obj;
            }
        }
        var last_score = playerPerfers.instance.getItem("lastScore");
        if(last_score){
            this.lastScore = parseInt(last_score);
        }
    },
    saveArrData(arrData){
        var str = JSON.stringify(arrData);
        playerPerfers.instance.setItem("arrdata_2",  str);
        playerPerfers.instance.setItem("lastScore",  this.curScore_2);
        
    },
    //
    getUIMgr(){
        if(!this.uiMgr || this.uiMgr.isValid == false){
            var obj = cc.find("Canvas");
            this.uiMgr = obj.getComponent("UIManager");
            return this.uiMgr;
        }
        return this.uiMgr;
    },
});

UserData.instance = new UserData();
module.exports = UserData;